﻿using AntdUI;
using AntdUI.In;
using Google.Protobuf.WellKnownTypes;
using PosClient.ViewModel;
using ReactiveUI;
using Splat;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PosClient.View
{
    public partial class SearchVipView : Window, IViewFor<SearchVipViewModel>
    {
        object IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (SearchVipViewModel)value;
        }
        public SearchVipViewModel ViewModel { get; set; }

        public SearchVipView()
        {
            InitializeComponent();
            InitSetting();
            InitEvnet();
            InitBinding();
        }

        private void InitSetting()
        {
            this.KeyPreview = true;
        }

        private void InitEvnet()
        {
            this.KeyDown += (s, e) =>
            {
                if (e.KeyData == Keys.Escape)
                {
                    this.Close();
                }
                else if (e.KeyData == Keys.Enter)
                {
                    if (ViewModel.Vip.VipCode != null)
                    {
                        this.Close();
                    }
                    else
                    {
                        TaskSearchVipAsync();
                    }
                }
            };

            this.button_search.Click += (s, e) =>
            {
                TaskSearchVipAsync();
            };
        }

        private void InitBinding()
        {
            ViewModel = new SearchVipViewModel(RxApp.MainThreadScheduler);
            this.WhenActivated(d =>
            {

                this.Events().Load
                    .Do(x => ActiveControl = input_code)
                    .Subscribe()
                    .DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.InputCode,
                    v => v.input_code.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Vip.VipCode,
                    v => v.label_code.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Vip.VipName,
                    v => v.label_name.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Vip.VipName,
                    v => v.label_type.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Vip.GrowthCurrent,
                    v => v.label_growth.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Vip.PointCurrent,
                    v => v.label_point.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Vip.TicketCount,
                    v => v.label_tickets.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Vip.DepositCurrent,
                    v => v.label_deposit.Text
                    ).DisposeWith(d);

                this.Bind(ViewModel,
                    vm => vm.Vip.Mobile,
                    v => v.label_mobile.Text
                    ).DisposeWith(d);

                this.ViewModel
                    .WhenAnyValue(vm => vm.Vip.Lables)
                    .Do(y =>
                    {
                        if (y != null && y.Count > 0)
                        {
                            Action action = () =>
                            {
                                flowPanel_lables.Controls.Clear();
                                foreach (var lable in y)
                                {
                                        AntdUI.Tag tag = new AntdUI.Tag();
                                        tag.CloseIcon = false;
                                        tag.Text = lable;
                                        tag.AutoSize = true;
                                        tag.Type = TTypeMini.Primary;
                                        flowPanel_lables.Controls.Add(tag);
                                }
                            };
                            flowPanel_lables.BeginInvoke(action);

                        }
                    }).Subscribe().DisposeWith(d);

                ViewModel.SearchVipCommand.Subscribe(result =>
                {
                    if (!result.Success)
                    {
                        var config1 = new AntdUI.Tooltip.Config(input_code, $"查询失败{result.Message}");
                        config1.ArrowAlign = AntdUI.TAlign.Top;
                        AntdUI.Tooltip.open(config1);
                    }
                    else
                    {
                        button_search.Loading = false;
                        var mainViewModel = Locator.Current.GetService<MainViewModel>();
                        mainViewModel.VipBase = result.Data;
                    }
                }).DisposeWith(d);

                this.BindCommand(ViewModel, vm => vm.SearchVipCommand, v => v.button_search).DisposeWith(d);

            });
        }

        private async void TaskSearchVipAsync()
        {
            button_search.Loading = true;
            var loadTask = Task.Run(() =>
            {
                ViewModel.SearchVipCommand.Execute().Subscribe();
            });
            await loadTask.ContinueWith(d =>
            {
                if (d.IsFaulted)//任务出错
                {
                    var config1 = new AntdUI.Tooltip.Config(input_code, $"查询失败{d.Exception.Message}");
                    config1.ArrowAlign = AntdUI.TAlign.Top;
                    AntdUI.Tooltip.open(config1);
                    button_search.Loading = false;
                }
                if (d.IsCompleted)//任务完成
                {
                    button_search.Loading = false;
                }
                button_search.Loading = false;
            });
        }
    }
}
